package cn.hfatec.shms.base;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseAction;
import cn.hfatec.constants.SysContant;
import cn.hfatec.shms.constants.ShmsConstant;
import cn.hfatec.shms.system.menu.model.MenuView;
import cn.hfatec.shms.system.menu.service.MenuService;
import cn.hfatec.shms.system.notice.model.NoticeParams;
import cn.hfatec.shms.system.notice.service.NoticeService;
import cn.hfatec.shms.system.user.model.User;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: panwb
 *
 * Date: 2015/11/3
 * Time: 10:01
 */
@Controller
public class ShmsBaseAction extends BaseAction {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name = "MenuService")
    private MenuService menuService;


    @Resource(name = "NoticeService")
    private NoticeService noticeService;


    @InitBinder
    public void initBinder(WebDataBinder binder){
        binder.registerCustomEditor(Date.class, new DateEditor());
    }

    /**
     *
     * 根据登录用户，获取菜单数据
     *
     * @return
     */
    @ModelAttribute(value = "userMenuList")
    public List listMenuByLoginUser(HttpServletRequest request) {
        try {
            User user = (User)request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if(user == null) {
                return Lists.newArrayList();
            }
            String uri = request.getRequestURI();
            synchronized (ShmsBaseAction.class) {
                if (request.getSession().getAttribute(ShmsConstant.MENU_CACHE) == null) {
                    List<MenuView> menuViewList = menuService.queryMenuListByLoginName(user.getLoginName());
                    request.getSession().setAttribute(ShmsConstant.MENU_CACHE, menuViewList);
                }
            }
            List<MenuView> menuViewList = (List)request.getSession().getAttribute(ShmsConstant.MENU_CACHE);
            for(MenuView menuView : menuViewList) {
                menuView.setIsSelected(false);
                for(MenuView childMenuView : menuView.getChildrenMenuList()) {
                    childMenuView.setIsSelected(false);
                    if(uri.contains(childMenuView.getMenuUrl())) {
                        childMenuView.setIsSelected(true);
                        menuView.setIsSelected(true);
                    }
                }
            }
            return menuViewList;
        } catch (Exception e) {
            logger.error("获取用户菜单数据异常", e);
        }
        return Lists.newArrayList();
    }

    /**
     *
     * 当前用户管理的客户列表
     *
     * @param request
     * @return
     */
    @ModelAttribute(value = "myManageCustomList")
    public List listCustomByLoginUser(HttpServletRequest request) {
        try {
            User user = (User)request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if(user == null) {
                return Lists.newArrayList();
            }
            String uri = request.getRequestURI();
        } catch (Exception e) {
            logger.error("获取用户菜单数据异常", e);
        }
        return Lists.newArrayList();
    }

    /**
     *
     * 根据登录用户，获取对应的最新6条系统消息
     *
     * @return
     */
    @ModelAttribute(value = "newsList")
    public List listNews(HttpServletRequest request) {
        try {
            User user = (User)request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if(user == null) {
                return Lists.newArrayList();
            }
            NoticeParams noticeParams = new NoticeParams();
            Page page = new Page();
            noticeParams.setReceiveUserId(user.getLoginName());
            page.setPageSize(6);
            page.setCurrentPage(1);
            Table table = noticeService.queryNoticeTable(noticeParams,page);
            List newsList=table.getRows();
            return newsList;
        } catch (Exception e) {
            logger.error("获取系统消息数据异常", e);
        }
        return Lists.newArrayList();
    }

    /**
     *
     * 根据登录用户，获取对应的所有系统消息
     *
     * @return
     */
    @ModelAttribute(value = "totalNewsList")
    public List listTotalNews(HttpServletRequest request) {
        try {
            User user = (User)request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
            if(user == null) {
                return Lists.newArrayList();
            }
            NoticeParams noticeParams = new NoticeParams();
            noticeParams.setReceiveUserId(user.getLoginName());
            noticeParams.setIsRead(ShmsConstant.IS_RED_NO);
            List totalNewsList = noticeService.queryNoticeList(noticeParams);
            return totalNewsList;
        } catch (Exception e) {
            logger.error("获取系统消息数据异常", e);
        }
        return Lists.newArrayList();
    }

    class DateEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            try {
                date = format.parse(text);
            } catch (ParseException e) {
                format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = format.parse(text);
                } catch (ParseException e1) {
                    //e1.printStackTrace();
                }
            }
            setValue(date);
        }
    }
}
